package connectfour;

import java.io.Serializable;
import java.util.Date;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.users.User;

@PersistenceCapable
public class Player implements Serializable {
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	Key key;
	
	@Persistent
	User player;
	
	@Persistent
	int wins;  // number of wins this player has
	
	@Persistent
	int losses;  // number of losses this player has
	
	@Persistent
	double ratio;  // win/loss ratio for this player
	
	@Persistent
	Date lastGamePlayed;  // date of the last game played by this player
	
	@Persistent
	int status; // 0 = not playing, 1 = host (p1), 2 = opponent (p2)
	
	
	public Player(User p){
		player = p;
		wins = 0;
		losses = 0;
		ratio = 0.0;
		status = 0;
	}
	
	public Player(Entity p){
		player = (User) p.getProperty("player");
		wins = (Integer) p.getProperty("wins");
		losses = (Integer) p.getProperty("losses");
		ratio = (Double) p.getProperty("ratio");
		status = (Integer) p.getProperty("status");
	}
	public Key getKey(){ return key; }
	public User getPlayer(){ return player; }
	public int getWins(){ return wins; }
	public int getLosses(){ return losses; }
	public double getRatio() { return ratio; }
	public Date getLastGamePlayed() { return lastGamePlayed; }
	public int getStatus() { return status; }
	
	public void setPlayer(User p){ player = p; }
	public void setWins(int w){ wins = w; }
	public void setLosses(int l){ losses = l; }
	public void setRatio(){ 
		if(wins == 0){
			ratio = 0.0;
		}
		else if(losses == 0){
			ratio = 1.0;
		}
		else{
			ratio = losses/wins;
		}
	}
	public void setLastGamePlayed(Date d) { lastGamePlayed = d; }
	public void setStatus(int s) { status = s; }
	public void incWins() { wins++; }
	public void incLosses() { losses++; }
} 
